/*
 * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package top.continew.admin.hanfu.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import net.dreamlu.mica.core.result.R;
import org.springframework.web.bind.annotation.*;
import top.continew.admin.hanfu.core.oss.OSS;
import top.continew.admin.hanfu.domain.Vneue;
import top.continew.admin.hanfu.domain.bo.VneueBo;
import top.continew.admin.hanfu.domain.dto.VneueDto;
import top.continew.admin.hanfu.domain.query.VneueQuery;
import top.continew.admin.hanfu.domain.vo.VneueVo;
import top.continew.admin.hanfu.domain.vo.wx.VneueWxVo;
import top.continew.admin.hanfu.service.impl.VneueServiceImpl;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * 我出幻觉了，vneue是场景
 *
 * @author bbm
 * @since 2025-04-06
 */
@RestController
@RequestMapping("/vneue")
@RequiredArgsConstructor
public class VneueController {

    private final VneueServiceImpl vneueService;

    private final OSS oss;

    @PostMapping("")
    public R addVneue(@RequestBody VneueDto vneueDto) {

        Vneue vneue = BeanUtil.copyProperties(vneueDto, Vneue.class);

        vneue.setUpdateTime(System.currentTimeMillis() + "");
        vneue.setStatus(1);
        boolean save = vneueService.save(vneue);

        return save ? R.success() : R.fail("新增失败");
    }

    @PostMapping("/inquire")
    public R InquireVneue(@RequestBody VneueQuery vneueQuery) {

        VneueBo bo = new VneueBo();

        bo.setDescription(vneueQuery.getDescription().isEmpty() ? null : vneueQuery.getDescription());
        bo.setStatus(vneueQuery.getStatus() == null ? null : vneueQuery.getStatus());

        Page<VneueVo> vneueVoPage = vneueService.InquiryVneue(new Page<VneueVo>(vneueQuery.getPageNum(), vneueQuery
            .getPageSize()), bo);

        return R.success(vneueVoPage);
    }

    @DeleteMapping("/{id}")
    public R deleteVneue(@PathVariable("id") Integer id) {
        Integer i = vneueService.deleteVneue(id);
        return i == 1 ? R.success() : R.fail("删除失败");
    }

    @PutMapping("")
    public R<Void> updateVneue(@RequestBody VneueDto vneueDto) {
        Vneue byId = vneueService.getById(vneueDto.getId());
        StringBuilder stringBuilder = new StringBuilder();
        String ossId = byId.getOssId();

        if (ossId != null && vneueDto.getOssId() != null) {
            List<String> newOssid = Arrays.stream(vneueDto.getOssId().split(",")).toList();
            List<String> old = Arrays.stream(ossId.split(",")).toList();

            List<String> list = old.stream().filter(i -> !newOssid.contains(i)).toList();

            oss.deleteFiles(list);

            newOssid.forEach(i -> {
                stringBuilder.append(i).append(",");
            });

        }

        stringBuilder.delete(stringBuilder.length() - 1, stringBuilder.length());

        boolean update = vneueService.lambdaUpdate()
            .set(!vneueDto.getDescription().isEmpty(), Vneue::getDescription, vneueDto.getDescription())
            .set(!vneueDto.getHistory().isEmpty(), Vneue::getHistory, vneueDto.getHistory())
            .set(Vneue::getOssId, stringBuilder.toString())
            .set(Vneue::getUpdateTime, System.currentTimeMillis() + "")
            .eq(Vneue::getId, vneueDto.getId())
            .update();

        return update ? R.success() : R.fail("更新失败");
    }

    @SaIgnore
    @PostMapping("/wx")
    public R<Page<VneueWxVo>> getVneueWx(@RequestBody VneueQuery vneueQuery) {
        VneueBo bo = new VneueBo();
        bo.setDescription(vneueQuery.getDescription());
        bo.setStatus(vneueQuery.getStatus() == null ? null : vneueQuery.getStatus());
        Page<VneueWxVo> vneueWxVoPage = vneueService.InquiryVneueWx(new Page<VneueWxVo>(vneueQuery
            .getPageNum(), vneueQuery.getPageSize()), bo);
        return R.success(vneueWxVoPage);
    }

}
